<template>
  <div class="xtx-goods-page" v-if="goodData.categories">
    <div class="container">
      <!-- 面包屑 -->
      <XtxBread>
        <XtxBreadItem to="/">首页</XtxBreadItem>
        <XtxBreadItem :to="`/category/${goodData.categories[1].id}`">{{
          goodData.categories[1].name
        }}</XtxBreadItem>
        <XtxBreadItem :to="`/subCategory/${goodData.categories[0].id}`">{{
          goodData.categories[0].name
        }}</XtxBreadItem>
        <XtxBreadItem>{{ goodData.name }}</XtxBreadItem>
      </XtxBread>
      <!-- 商品信息 -->
      <div class="goods-info">
        <!-- 图片预览区 -->
        <div class="media">
          <GoodImage :image-list="goodData.mainPictures" />
        </div>
        <!-- 商品信息区 -->
        <div class="spec">
          <!-- sku组件 -->
          <SKU />
          <!-- 数量组件 -->
          <XtxNumbox :min="1" :max="9" />
          <!-- 按钮组件 -->
          <XtxButton size="middle" type="primary">加入购物车</XtxButton>
        </div>
      </div>
      <!-- 商品详情 -->
      <div class="goods-footer">
        <div class="goods-article">
          <!-- 商品+评价 -->
          <div class="goods-tabs">
            <GoodTabs />
          </div>
        </div>
        <!-- 24热榜+专题推荐 -->
        <div class="goods-aside">
          <GoodHot :type="1" />
          <GoodHot :type="2" />
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import GoodImage from './components/good-image.vue'
import { findGoods } from '@/api/good'
import { onMounted, ref } from 'vue'
import { useRoute } from 'vue-router'
import SKU from './Sku/index.vue'
import GoodTabs from './components/good-tabs.vue'
import GoodHot from './components/good-hot.vue'
import { useStore } from 'vuex'
export default {
  name: 'XtxGoodsPage',
  components: {
    GoodImage, SKU, GoodTabs, GoodHot
  },
  setup () {
    const goodData = ref({})
    const route = useRoute()
    async function loadDoodData () {
      const res = await findGoods(route.params.id)
      goodData.value = res.result
    }
    onMounted(() => {
      loadDoodData()
    })
    const skuData = ref({})
    // 加入购物车
    const num = ref(1)
    const store = useStore()
    function insertCart (sku) {
      // const good = {
      //   id: goods.value.id,
      //   name: goods.value.name,
      //   picture: goods.value.mainPictures[0],
      //   skuId: currSku.value.skuId,
      //   price: currSku.value.oldPrice,
      //   nowPrice: currSku.value.price,
      //   attrsText: currSku.value.specsText,
      //   stock: currSku.value.inventory,
      //   selected: true,
      //   isEffective: true,
      //   count: num.value
      // }
      store.dispatch('cart/getInsertCart')
      if (sku.skuId) {
        skuData.value = sku
      }
    }
    return {
      goodData, num, insertCart
    }
  }
}
</script>

<style scoped lang='less'>
.goods-info {
  min-height: 600px;
  background: #fff;
  display: flex;
  .media {
    width: 580px;
    height: 600px;
    padding: 30px 50px;
  }
  .spec {
    flex: 1;
    padding: 30px 30px 30px 0;
  }
}
.goods-footer {
  display: flex;
  margin-top: 20px;
  .goods-article {
    width: 940px;
    margin-right: 20px;
  }
  .goods-aside {
    width: 280px;
    min-height: 1000px;
  }
}
.goods-tabs {
  min-height: 600px;
  background: #fff;
}
.goods-warn {
  min-height: 600px;
  background: #fff;
  margin-top: 20px;
}
</style>
